package com.bgba.oer;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

import com.flashline.registry.openapi.base.OpenAPIException;
import com.flashline.registry.openapi.entity.Asset;
import com.flashline.registry.openapi.entity.AssetSummary;
import com.flashline.registry.openapi.entity.AuthToken;
import com.flashline.registry.openapi.entity.RelationshipType;
import com.flashline.registry.openapi.query.AssetCriteria;
import com.flashline.registry.openapi.query.SearchTerm;
import com.flashline.registry.openapi.service.v300.FlashlineRegistry;
import com.flashline.registry.openapi.service.v300.FlashlineRegistryServiceLocator;

public class AddRelationshipTest {


	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("======================== ");
		String urlConnectionLogin = "http://desasoa01/oer/services/FlashlineRegistry?WSDL";
		String username = " L0269239";
		String password = "finde2011";
		/*
		String urlConnectionLogin = "http://172.17.1.10:8101/oer/services/FlashlineRegistry?WSDL";
		String username = " admin";
		String password = "admin123";
		*/
		
		AddRelationshipTest instance = new AddRelationshipTest();
		instance.addRelationships(urlConnectionLogin, username, password);

		//addRelationships(urlConnectionLogin, username, password);
	}

	private  void addRelationships(String urlConnectionLogin, String username, String password) {
		try {
			///////////////////////////////////////////////////////////
			// Connect to Oracle Enterprise Repository
			///////////////////////////////////////////////////////////
			URL lURL = null;
			lURL = new URL(urlConnectionLogin);
			FlashlineRegistry repository = new FlashlineRegistryServiceLocator().getFlashlineRegistry(lURL);

			///////////////////////////////////
			// Login to OER
			///////////////////////////////////
			AuthToken authToken = repository.authTokenCreate(username, password);

			//////////////////////////////////////
			// asset name for operation
			//////////////////////////////////////
			String assetNameCriteria = "Operacion - Test";
			//String assetNameCriteria = "operacion programatica";
			//String assetNameCriteria = "Operation - Test SAP";			

			Asset myOperationAsset = searchAssetByName(repository, authToken, assetNameCriteria);

			System.out.println("========================= myOperationAsset.getID="+myOperationAsset.getID());
			System.out.println("========================= myOperationAsset.getDisplayName="+myOperationAsset.getDisplayName());
			System.out.println("========================= myOperationAsset.getTypeName="+myOperationAsset.getTypeName());

			RelationshipType[] allRelationshipTypes = myOperationAsset.getRelationshipTypes();
			System.out.println("========================= allRelationshipTypes.length=="+allRelationshipTypes.length);

			RelationshipType myRelationshipType = null;
			String relationshipName ="Provides"; 
			for (RelationshipType relationshipType : allRelationshipTypes) {
				//System.out.println("=========================== relationshipType.getName="+relationshipType.getName());
				if (relationshipType.getName().equals(relationshipName)){
					//This is the relationship type, modify the assets that are related
					// using it
					myRelationshipType = relationshipType;
					break;
				}
			}
			
			if (myRelationshipType == null ){
				System.out.println("======================================= NOT FOUND Relationship TYPE for assets with name="+assetNameCriteria);				
			}
			
			if (myRelationshipType.getName().equals(relationshipName)){
				if ((myRelationshipType.getPrimaryIDs().length > 0) || (myRelationshipType.getSecondaryIDs().length > 0)){
					System.out.println("================================= myRelationshipType.getDisplayName()="+myRelationshipType.getDisplayName());
					System.out.println("================================= myRelationshipType.getDisplaySecondary()="+myRelationshipType.getDisplaySecondary());

					if (myRelationshipType.getPrimaryIDs().length > 0){
						for (Long relationshipPrimaryId : myRelationshipType.getPrimaryIDs()) {
							System.out.println("====================================== relationshipPrimaryId="+relationshipPrimaryId);
						}
					} else {
						System.out.println("======================================= NOT FOUND Relationship PRIMARY for assets with name="+assetNameCriteria);				
					}

					if (myRelationshipType.getSecondaryIDs().length > 0){
						for (Long relationshipSecondaryId : myRelationshipType.getSecondaryIDs()) {
							System.out.println("====================================== relationshipSecondaryId="+relationshipSecondaryId);
						}
					} else {
						System.out.println("======================================= NOT FOUND Relationship SECONDARY for assets with name="+assetNameCriteria);				
					}
				}
			}
			
			
			//////////////////////////////////////
			// asset name for service
			//////////////////////////////////////
			assetNameCriteria = "SAP Banking";
			//assetNameCriteria = "Sample Service - Account Detail";
			//"Operation - Test Alta Cliente"

			Asset myServiceAsset = searchAssetByName(repository, authToken, assetNameCriteria);

			System.out.println("========================= myServiceAsset.getID="+myServiceAsset.getID());
			System.out.println("========================= myServiceAsset.getDisplayName="+myServiceAsset.getDisplayName());
			System.out.println("========================= myServiceAsset.getTypeName="+myServiceAsset.getTypeName());
			
			
			//myOperationAsset.setC
			/*

			if (myServiceAsset.getRelationshipTypes().length > 0 ){
				for ( RelationshipType relationshipType : myServiceAsset.getRelationshipTypes()) {
					//System.out.println("================================= relationshipType");
					//System.out.println("================================= relationshipType.getID()="+relationshipType.getID());
					//System.out.println("================================= relationshipType.getName()="+relationshipType.getName());
					//System.out.println("================================= relationshipType.getPrimaryIDs().length="+relationshipType.getPrimaryIDs().length);
					//System.out.println("================================= relationshipType.getSecondaryIDs().length="+relationshipType.getSecondaryIDs().length );

					if (relationshipType.getName().equals(relationshipName)){

						if ((relationshipType.getPrimaryIDs().length > 0) || (relationshipType.getSecondaryIDs().length > 0)){
							System.out.println("================================= relationshipType.getDisplayName()="+relationshipType.getDisplayName());
							System.out.println("================================= relationshipType.getDisplaySecondary()="+relationshipType.getDisplaySecondary());

							if (relationshipType.getPrimaryIDs().length > 0){
								for (Long relationshipPrimaryId : relationshipType.getPrimaryIDs()) {
									System.out.println("====================================== relationshipPrimaryId="+relationshipPrimaryId);
								}
							} else {
								System.out.println("======================================= NOT FOUND Relationship PRIMARY for assets with name="+assetNameCriteria);				
							}

							if (relationshipType.getSecondaryIDs().length > 0){
								for (Long relationshipSecondaryId : relationshipType.getSecondaryIDs()) {
									System.out.println("====================================== relationshipSecondaryId="+relationshipSecondaryId);
								}
							} else {
								System.out.println("======================================= NOT FOUND Relationship SECONDARY for assets with name="+assetNameCriteria);				
							}
						}
					}
				}
			} else {
				System.out.println("======================================= NOT FOUND RelationshipTypes for assets with name="+assetNameCriteria);				
			}
			*/

			//add this asset to the list of related assets
			long[] oldPrimaryIDs = myRelationshipType.getPrimaryIDs();
			long[] newPrimaryIDs = new long[oldPrimaryIDs.length + 1];
			for (int j = 0; j < oldPrimaryIDs.length; j++) {
				newPrimaryIDs[j] = oldPrimaryIDs[j];
			}
			//newPrimaryIDs[newPrimaryIDs.length - 1] = myServiceAsset.getID();
			//newPrimaryIDs[newPrimaryIDs.length - 1] = new Long(50239).longValue();
			myRelationshipType.setPrimaryIDs(newPrimaryIDs);

			myOperationAsset.setRelationshipTypes(allRelationshipTypes);
			System.out.println("======================================= antes del update ");
			//repository.assetUpdate(authToken, myOperationAsset);			
			

			System.out.println("======================================= ");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	private Asset searchAssetByName(FlashlineRegistry repository, AuthToken authToken, String assetNameCriteria) throws RemoteException, OpenAPIException {
		System.out.println("\n ######################################################");
		///////////////////////////////////////////////////////////
		// Search assets
		// try a general search which includes Name, version,
		// description, and keywords
		////////////////////////////////////////////////////////////

		AssetCriteria criteria = new AssetCriteria();
		criteria = new AssetCriteria();
		criteria.setGeneralCriteria(assetNameCriteria);

		AssetSummary[] assets = repository.assetQuerySummary(authToken, criteria);
		//criteria.
		System.out.println("======================================= assets="+assets);
		System.out.println("======================================= assets.length="+assets.length);
		Asset myAsset = null;
		if (assets.length == 1){
			AssetSummary assetSummary = assets[0];
			//System.out.println("========================= assetSummary.getID="+assetSummary.getID());
			//System.out.println("========================= assetSummary.getName="+assetSummary.getName());
			//System.out.println("========================= assetSummary.getVersion="+assetSummary.getVersion());
			//System.out.println("========================= assetSummary.getTypeID="+assetSummary.getTypeID());

			myAsset = repository.assetRead(authToken, assetSummary.getID());

			//System.out.println("========================= myAsset.getDisplayName="+myAsset.getDisplayName());
			//System.out.println("========================= myAsset.getTypeName()="+myAsset.getTypeName());
			//System.out.println("========================= myAsset.getTypeName()="+myAsset.getTypeName());

		} else {
			System.out.println("======================================= NOT FOUND assets with name="+assetNameCriteria + ", assets resultSet="+assets.length);				
		}

		return myAsset;
	}

}
